﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataStructDemo
{
    public class SequenceListDemo<T> : IListDS<T>
    {
        private T[] _data;
        private int _maxSize;
        private int _lastPosition;

        public SequenceListDemo(int size)
        {
            _maxSize = size;
            _data = new T[size];
            _lastPosition = -1;
        }

        public T this[int index]
        {
            get { return _data[index]; }
            set { _data[index] = value; }
        }

        public int Last
        {
            get { return _lastPosition; }
        }

        public int Getlength()
        {
            return _lastPosition + 1;
        }

        public bool IsEmpty()
        {
            if (_lastPosition == -1)
            {
                return true;
            }
            return false;
        }
        public bool IsFull()
        {
            if (_lastPosition + 1 == _maxSize)
            {
                return true;
            }
            return false;
        }

        public void Clear()
        {
            _lastPosition = 0;
        }

        public bool AppendTo(T item)
        {
            if (IsFull())
            {
                throw new Exception("数组已满！");
                return false;
            }
            _data[++_lastPosition] = item;
            return true;
        }

        public bool Insert(T item, int i)
        {
            if (IsFull())
            {
                Console.Write("顺序表中数据元素已满，无法继续插入，操作失败！");
                return false;
            }
            else
            {
                if (i < 1 || i > _lastPosition + 2)
                {
                    Console.Write("超出顺序表范围，无法插入，操作失败！");
                    return false;
                }
                else if (i == _lastPosition + 2)
                {
                    _data[_lastPosition + 1] = item;
                    _lastPosition++;
                }
                else
                {
                    for (int j = _lastPosition; j < i - 1; j--)
                    {
                        _data[j + 1] = _data[j];
                    }
                    _data[i - 1] = item;
                    _lastPosition++;
                }
                return true;
            }
        }

        public T Delete(int i)
        {
            T temp = default(T);
            if (IsEmpty())
            {
                Console.Write("顺序表中已空，没有可以删除的元素！");
            }
            else if(i<1&&i>_lastPosition+1)
            {
                Console.Write("非法参数，没有可以操作的对象");
            }
            else
            {
                temp=_data[i];
                for (int j = i-1; j < _lastPosition; j++)
                {

                    _data[j] = _data[j + 1];
                }
                _maxSize--;
            }
            return temp;
        }

        public T GetElem(T data)
        {
            throw new NotImplementedException();
        }



        public T Locate(T value)
        {
            throw new NotImplementedException();
        }
    }
}
